package com.hwhhhh.mygrad;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.adapter.CommentAdapter;
import com.hwhhhh.mygrad.api.Api;
import com.hwhhhh.mygrad.api.ApiCallback;
import com.hwhhhh.mygrad.api.ApiConfig;
import com.hwhhhh.mygrad.dto.CommentDTO;
import com.hwhhhh.mygrad.entity.RespDataArr;
import com.hwhhhh.mygrad.interfaces.CommentListener;
import com.hwhhhh.mygrad.utils.StringUtils;
import com.hwhhhh.mygrad.view.RoundImageView;

import java.util.HashMap;
import java.util.List;

public class CommentActivity extends BaseActivity implements View.OnClickListener{
    private RoundImageView profilePhoto;
    private TextView tvName, tvDate, tvContent, tvLike, tvReply;
    private CommentDTO commentDTO;
    private RecyclerView recyclerView;
    private int page = 0;
    private List<CommentDTO> childCommentList;
    private EditText etComment;
    private CommentAdapter commentAdapter;

    @Override
    protected void initView() {
        profilePhoto = findViewById(R.id.head_portrait);
        tvName = findViewById(R.id.tv_comment_name);
        tvDate = findViewById(R.id.tv_comment_date);
        tvContent = findViewById(R.id.tv_comment_content);
        tvLike = findViewById(R.id.tv_like);
        tvReply = findViewById(R.id.tv_reply);
        recyclerView = findViewById(R.id.rv_comment_list);
        etComment = findViewById(R.id.et_cls_comment);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_comment;
    }

    @Override
    protected void initData() {
        Intent in = getIntent();
        commentDTO = (CommentDTO) in.getSerializableExtra("commentParent");
        // 是否为发布评论而进入的页面
        boolean isNew = in.getBooleanExtra("newComment", false);
        if (isNew) showToast("评论成功！积分+1");
        requestData();
        Glide.with(this).load(commentDTO.getuProfilePhoto()).into(profilePhoto);
        tvName.setText(commentDTO.getUname());
        tvContent.setText(commentDTO.getContent());
        String str = "点赞" + "（" + commentDTO.getLikes() + "）";
        tvLike.setText(str);
        tvReply.setOnClickListener(this);
        etComment.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etComment.clearFocus();
                showCommentFragment(commentDTO.getClassroomId(), commentDTO.getId(), commentDTO.getContent())
                        .setCommentListener(new CommentListener() {
                            @Override
                            public void onSuccess(CommentDTO c) {
                                CommentActivity.this.runOnUiThread(() -> {
                                    showToast("评论成功！积分+1");
                                    commentAdapter.insertTo(0, c);
                                    commentAdapter.notifyItemInserted(0);
                                });
                            }

                            @Override
                            public void onFail() {

                            }
                        });
            }
        });
    }

    private void requestData() {
        // 请求评论区信息
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        Api.config(ApiConfig.COMMENT + "/" + commentDTO.getId(), params).getRequest(this, new ApiCallback() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onSuccess(String res) {
                RespDataArr<CommentDTO> data = StringUtils.fromJsonArr(res, new TypeToken<RespDataArr<CommentDTO>>(){}.getType());
                if (data.getCode() == 200) {
                    childCommentList = data.getData();
                    CommentActivity.this.runOnUiThread(() -> {
                        // 评论区
                        commentAdapter = new CommentAdapter(CommentActivity.this, childCommentList);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CommentActivity.this);
                        recyclerView.setAdapter(commentAdapter);
                        recyclerView.setLayoutManager(linearLayoutManager);
                    });
                } else {
                    showToastSync(data.getMsg());
                }
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("request fail");
            }
        });

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_reply) {
            showCommentFragment(commentDTO.getClassroomId(), commentDTO.getId(), commentDTO.getContent())
                    .setCommentListener(new CommentListener() {
                        @Override
                        public void onSuccess(CommentDTO c) {
                            CommentActivity.this.runOnUiThread(() -> {
                                showToast("评论成功！积分+1");
                                commentAdapter.insertTo(0, c);
                                commentAdapter.notifyItemInserted(0);
                            });
                        }

                        @Override
                        public void onFail() {

                        }
                    });
        }
    }
}